package be.ac.ua.pats.thesisrobotica.robot.RR.sensors;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.RR.Reflective;
import be.ac.ua.pats.thesisrobotica.robot.communication.Connection;
import be.ac.ua.pats.thesisrobotica.robot.constants.RobotConstants;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.InitializationException;

import lejos.nxt.SensorPort;
import lejos.nxt.comm.RConsole;

/**
 * Implementation of the touchsensor
 *
 * @author Jimmy
 */
public class TouchSensor extends Sensor implements Reflective {
    public lejos.nxt.TouchSensor sensor;

    public TouchSensor(SensorPort port) {
        this(null, port);
    }

    /**
     * @param connection The connection over which notifications should be sent
     */
    public TouchSensor(Connection connection, SensorPort port) {
        super(connection);

        sensor      = new lejos.nxt.TouchSensor(port);
        type        = RobotConstants.Types.TOUCHSENSOR;
        initialized = true;
    }

    /**
     * @return The current sensor reading
     */
    @Override
    public int getRealValue() throws InitializationException {
        if (!initialized) {
            throw new InitializationException(this.id);
        }
        return (((lejos.nxt.TouchSensor) sensor).isPressed())
               ? 1
               : 0;
    }

    @Override
    public String getValue() throws InitializationException {
        if (!initialized) {
            throw new InitializationException(this.id);
        }

        return sensor.isPressed()
               ? "1"
               : "0";
    }
}
